package com.asiainfo.work.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import java.lang.reflect.Type;
import java.util.List;
import org.modelmapper.TypeToken;

/**
 * 将MyBatisPlug page转换为PageResponse
 * @author: bf
 * @date: 2021/1/5-10:20
 */
public class PageResponseUtil {

    /**
     * List类型对象转换
     *
     * @param t
     * @param <T>
     * @return
     */
    @Deprecated
    public static <T> Type listType(T t) {
        return new TypeToken<List<T>>() {
        }.getType();
    }

    /**
     * 将MyBatisPlug page转换为PageResponse
     *
     * @param page
     * @return
     */
    public static <T> PageResponse<T> getPageResponse(IPage<T> page) {
        if (page == null) {
            return null;
        }
        PageResponse.PageInfo pageInfo = new PageResponse.PageInfo();
        pageInfo.setCurrentPage(page.getCurrent());
        pageInfo.setPageSize(page.getSize());
        pageInfo.setTotalNumber(page.getTotal());
        pageInfo.setTotalPage(page.getPages());

        PageResponse<T> response = new PageResponse<T>();
        response.setPageData(page.getRecords());
        response.setPageInfo(pageInfo);
        return response;
    }

}
